Public Class frmBaoCaoTongHopHangTangGiamPhamChat

    Private Sub frmBaoCaoTongHopHangTangGiamPhamChat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbbLoaiBaoCao.SelectedIndex = 0
        rbtKyQT.Checked = True
        KhoiTaocbbKyQuyetToan()
        cbbKYQT.SelectedIndex = 0
    End Sub

    Public Sub KhoiTaocbbKyQuyetToan()
        Dim ds As DataSet = BaseDB.ExecSql_DataSet("select * from tblKyQT")
        Dim row As DataRow
        row = ds.Tables(0).NewRow
        row("id") = 0
        row("tenkyqt") = "Cả Năm"
        ds.Tables(0).Rows.Add(row)
        cbbKyQT.DisplayMember = "tenkyqt"
        cbbKYQT.ValueMember = "id"
        cbbKYQT.DataSource = ds.Tables(0)
    End Sub

    Private Sub btnBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaoCao.Click
        If (cbbLoaiBaoCao.SelectedIndex <> -1) Then
            Dim th_hang1, th_hang2, th_hang3, tb_hang1, tb_hang2, tb_hang3, dc_hang1, dc_hang2, dc_hang3, tong_cong As Double
            Dim th_hang4, th_hang5, tb_hang4, tb_hang5, dc_hang4, dc_hang5, th_hang6, tb_hang6, dc_hang6 As Double
            Dim t_hc_bb, tb, dc, loai_baocao As String
            t_hc_bb = "Y0500000000000"
            tb = "Y0100000000000"
            dc = "Y0200000000000"
            Dim hang_nhapkhac, hang_xuatkhac, hang_tangphamcap, hang_giamphamcap As String

            Dim dcgia_nhap, hang_thuhoi, hang_thua, dc_chatluong_nhap As String
            Dim dcgia_xuat, hang_huy, hang_thieu, dc_chatluong_xuat As String

            'nhap
            dcgia_nhap = "001006"
            hang_thuhoi = "001007"
            hang_thua = "001009"
            hang_nhapkhac = "001099"
            dc_chatluong_nhap = "001018"
            hang_tangphamcap = "001008"
            'xuat
            dcgia_xuat = "004005"
            hang_huy = "004006"
            hang_thieu = "004008"
            hang_xuatkhac = "004099"
            dc_chatluong_xuat = "004018"
            hang_giamphamcap = "004007"
            If (cbbLoaiBaoCao.SelectedIndex = 0) Then 'hang tang pham cap
                loai_baocao = "TangPhamCap"
                th_hang1 = TongTienTheoNhomvaThoiGian(t_hc_bb, hang_thua)
                th_hang2 = TongTienTheoNhomvaThoiGian(t_hc_bb, dcgia_nhap)
                th_hang3 = TongTienTheoNhomvaThoiGian(t_hc_bb, hang_thuhoi)
                th_hang4 = TongTienTheoNhomvaThoiGian(t_hc_bb, hang_nhapkhac)
                th_hang5 = TongTienTheoNhomvaThoiGian(t_hc_bb, dc_chatluong_nhap)
                th_hang6 = TongTienTheoNhomvaThoiGian(t_hc_bb, hang_tangphamcap)
                '
                tb_hang1 = TongTienTheoNhomvaThoiGian(tb, hang_thua)
                tb_hang2 = TongTienTheoNhomvaThoiGian(tb, dcgia_nhap)
                tb_hang3 = TongTienTheoNhomvaThoiGian(tb, hang_thuhoi)
                tb_hang4 = TongTienTheoNhomvaThoiGian(tb, hang_nhapkhac)
                tb_hang5 = TongTienTheoNhomvaThoiGian(tb, dc_chatluong_nhap)
                tb_hang6 = TongTienTheoNhomvaThoiGian(tb, hang_tangphamcap)
                '
                dc_hang1 = TongTienTheoNhomvaThoiGian(dc, hang_thua)
                dc_hang2 = TongTienTheoNhomvaThoiGian(dc, dcgia_nhap)
                dc_hang3 = TongTienTheoNhomvaThoiGian(dc, hang_thuhoi)
                dc_hang4 = TongTienTheoNhomvaThoiGian(dc, hang_nhapkhac)
                dc_hang5 = TongTienTheoNhomvaThoiGian(dc, dc_chatluong_nhap)
                dc_hang6 = TongTienTheoNhomvaThoiGian(dc, hang_tangphamcap)

                '
            Else 'hang giam pham cap 
                loai_baocao = "GiamPhamCap"
                th_hang1 = TongTienTheoNhomvaThoiGian(t_hc_bb, hang_thieu)
                th_hang2 = TongTienTheoNhomvaThoiGian(t_hc_bb, dcgia_xuat)
                th_hang3 = TongTienTheoNhomvaThoiGian(t_hc_bb, hang_huy)
                th_hang4 = TongTienTheoNhomvaThoiGian(t_hc_bb, hang_xuatkhac)
                th_hang5 = TongTienTheoNhomvaThoiGian(t_hc_bb, dc_chatluong_xuat)
                th_hang6 = TongTienTheoNhomvaThoiGian(t_hc_bb, hang_giamphamcap)
                '
                tb_hang1 = TongTienTheoNhomvaThoiGian(tb, hang_thieu)
                tb_hang2 = TongTienTheoNhomvaThoiGian(tb, dcgia_xuat)
                tb_hang3 = TongTienTheoNhomvaThoiGian(tb, hang_huy)
                tb_hang4 = TongTienTheoNhomvaThoiGian(tb, hang_xuatkhac)
                tb_hang5 = TongTienTheoNhomvaThoiGian(tb, dc_chatluong_xuat)
                tb_hang6 = TongTienTheoNhomvaThoiGian(tb, hang_giamphamcap)
                '
                dc_hang1 = TongTienTheoNhomvaThoiGian(dc, hang_thieu)
                dc_hang2 = TongTienTheoNhomvaThoiGian(dc, dcgia_xuat)
                dc_hang3 = TongTienTheoNhomvaThoiGian(dc, hang_huy)
                dc_hang4 = TongTienTheoNhomvaThoiGian(dc, hang_xuatkhac)
                dc_hang5 = TongTienTheoNhomvaThoiGian(dc, dc_chatluong_xuat)
                dc_hang6 = TongTienTheoNhomvaThoiGian(dc, hang_giamphamcap)
            End If
            '
            tong_cong = th_hang1 + th_hang2 + th_hang3 + th_hang4 + th_hang5 + th_hang6
            tong_cong += tb_hang1 + tb_hang2 + tb_hang3 + tb_hang4 + tb_hang5 + tb_hang6
            tong_cong += dc_hang1 + dc_hang2 + dc_hang3 + dc_hang4 + dc_hang5 + dc_hang6
            '
            Dim nnreport As New rptTongHopHangTangGiamPhamCap
            nnreport.SetParameterValue("loai_baocao", loai_baocao)
            '
            nnreport.SetParameterValue("thuoc_hang1", th_hang1)
            nnreport.SetParameterValue("thuoc_hang2", th_hang2)
            nnreport.SetParameterValue("thuoc_hang3", th_hang3)
            nnreport.SetParameterValue("thuoc_hang4", th_hang4)
            nnreport.SetParameterValue("thuoc_hang5", th_hang5)
            nnreport.SetParameterValue("thuoc_hang6", th_hang6)
            '
            nnreport.SetParameterValue("trangbi_hang1", tb_hang1)
            nnreport.SetParameterValue("trangbi_hang2", tb_hang2)
            nnreport.SetParameterValue("trangbi_hang3", tb_hang3)
            nnreport.SetParameterValue("trangbi_hang4", tb_hang4)
            nnreport.SetParameterValue("trangbi_hang5", tb_hang5)
            nnreport.SetParameterValue("trangbi_hang6", tb_hang6)
            '
            nnreport.SetParameterValue("dungcu_hang1", dc_hang1)
            nnreport.SetParameterValue("dungcu_hang2", dc_hang2)
            nnreport.SetParameterValue("dungcu_hang3", dc_hang3)
            nnreport.SetParameterValue("dungcu_hang4", dc_hang4)
            nnreport.SetParameterValue("dungcu_hang5", dc_hang5)
            nnreport.SetParameterValue("dungcu_hang6", dc_hang6)
            '

            nnreport.SetParameterValue("tienchu", DoiSoRaChu(tong_cong))
            Dim KyQT As String
            If (cbbKYQT.SelectedValue = 0) Then 'ca nam
                KyQT = "Năm " + CStr(Now.Year)
            Else
                KyQT = cbbKYQT.Text + " - Năm " + CStr(Now.Year)
            End If

            If rbtKyQT.Checked Then
                nnreport.SetParameterValue("KYQT", KyQT)
            Else
                nnreport.SetParameterValue("KYQT", "Từ ngày " + dtpTuNgay.Value.ToString("dd/MM/yyyy") + " đến ngày " + dtpDenNgay.Value.ToString("dd/MM/yyyy"))
            End If

            'nnreport.SetParameterValue("KYQT", KyQT)
            Dim frm As New frmReportViewer(nnreport)
            frm.ShowDialog()
        Else
            ShowMessages("Bạn chưa chọn loại báo cáo.")
        End If
    End Sub

    Public Function TongTienTheoNhomvaThoiGian(ByVal loainhom As String, ByVal tcnx As String) As Double
        Dim strsql As String
        strsql = "select sum(TGIATRI) from tblDPNX where TCNX = '" & tcnx & "' and"
        If (loainhom = "Y0500000000000") Then 'thuoc, hoachat,bb
            strsql &= " ( LOAIMH = 'Y0500000000000' or LOAIMH = 'Y0600000000000' or LOAIMH = 'Y0700000000000' or LOAIMH = 'Y0800000000000')"
        Else
            strsql &= " LOAIMH ='" & loainhom & "'"
        End If
      
        If rbtKyQT.Checked Then
            If (cbbKYQT.SelectedValue <> 0) Then 'KyQT = Ca Nam
                strsql &= " AND KYQT = '" & cbbKYQT.SelectedValue & "'"
            End If
        Else
            strsql &= " AND NGAY >= '" & dtpTuNgay.Value.ToString("MM/dd/yyyy") & "' AND NGAY <= '" & dtpDenNgay.Value.ToString("MM/dd/yyyy") & "'"
        End If

        
        Dim value As Object
        value = BaseDB.ExecSql_DataValue(strsql)
        Dim ket_qua As Double
        If (Not value Is DBNull.Value) Then
            ket_qua = value
        Else
            ket_qua = 0
        End If
        Return ket_qua
    End Function

    Private Sub rbtKyQT_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtKyQT.CheckedChanged
        If rbtKyQT.Checked Then
            lblTuNgay.Visible = False
            lblDenNgay.Visible = False
            dtpTuNgay.Visible = False
            dtpDenNgay.Visible = False
            lblKyQT.Visible = True
            cbbKYQT.Visible = True
        Else
            lblTuNgay.Visible = True
            lblDenNgay.Visible = True
            dtpTuNgay.Visible = True
            dtpDenNgay.Visible = True
            lblKyQT.Visible = False
            cbbKYQT.Visible = False
        End If
    End Sub
End Class